<?php 
header('Content-type:text/html;charset=utf-8');
  $input = file_get_contents("php://input"); //½ÓÊÕPOSTÊý¾Ý

 
    echo "----------input--------<br />";
    var_dump(file_get_contents('php://input', 'r'));
    echo "----------post---------<br />";
    var_dump($_POST);

	 file_put_contents('txt/sbsjplbh.txt',$input);

     echo "string";
    //  {
    //  	"notifyType":"deviceDatasChanged",
    //  	"requestId":null,
    //  	"deviceId":"e023fea4-6e29-41e1-8b92-1bf47134335a",
    //  	"gatewayId":"e023fea4-6e29-41e1-8b92-1bf47134335a",
    //  	"services":[
	   //   	{
	   //   		"serviceId":"SQVH75",
		  //    	"serviceType":"SQVH75",
		  //    	"data":{
		  //    		"Alarm_on":0,
		  //    		"Alarm_fire":0,
		  //    		"Alarm_check":0,
		  //    		"Alarm_battery":0,
		  //    		"Alarm_fault":0,
		  //    		"Base_apart":1,
		  //    		"Base_lost":0,
		  //    		"Base_temperature":0,
		  //    		"Base_battery":0,
		  //    		"Base_Other":0
		  //    	},
	   //   		"eventTime":"20180411T042956Z"
    	//  	}
    //  	]
    // }
        if($input){
	    $arr = json_decode($input,true);
		    if(array_key_exists('services', $arr)&&array_key_exists('data', $arr['services'][0])&&array_key_exists('Alarm_check', $arr['services'][0]['data'])&&array_key_exists('Alarm_fire', $arr['services'][0]['data'])){
		//     // if(array_key_exists('notifyType',$arr) && ($arr['notifyType']=='deviceDataChanged')){
				if(($arr['services'][0]['data']['Alarm_check']==1)||($arr['services'][0]['data']['Alarm_fire']==1)||($arr['services'][0]['data']['Base_apart']==1)) {
					$eventTime = $arr['services'][0]['eventTime'];
					$time = substr($eventTime,0,4).'-'.substr($eventTime,4,2).'-'.substr($eventTime,6,2).' '.substr($eventTime,9,2).':'.substr($eventTime,11,2).':'.substr($eventTime,13,2);
					$datar='';
					file_put_contents('txt/11111.txt',$time);
					if($arr['services'][0]['data']['Base_apart']==1){
						$datar = 'b18010ff0000e1';
					}
					if($arr['services'][0]['data']['Alarm_check']==1){
						$datar = 'b18080ff0000e1';
					}
					if($arr['services'][0]['data']['Alarm_fire']==1){
						$datar = 'b18800ff0000e1';
					}
					if($datar != ''){
					    $data = [
				            'hd'=>[
				                'ver'=>'0',
				                'rdTk'=>'1234',
				                'pktp'=>0,
				                'gwad'=>"T6CN470S0019" //
				            ],
				            'rxpk'=>[
				                'time'=>$time, //
			                	'tmst'=>strtotime($time), //
				                'freq'=>472.9, //
				                'datr'=>'SF12BW125', //
				                'rssi'=>'-93.0', //
				                'lsnr'=>'17.9', //
				                'chan'=>7, //
				                'mote'=>[
				                    'DevE'=>'0000000000000391', //
				                    'DevA'=>913,
				                    'AppE'=>'BB02000000AB1111', //
				                    'fcnt'=>626, //
				                    'fprt'=>2, //
				                    'fmtp'=>2,
				                    'data'=>'+hZMSDA1MTQxMDAwMDAwMDAwMzkxEAD8',
				                    // 'datar'=>'b18001ff0000e1' //
				                    'datar'=>$datar //
				                ]
				            ],
				            'action'=>'app_data'
				        ];
						to_url('https://nb.777iot.com:443/api/nb',http_build_query($data));	
					}	
		    	}
		    }
		}
	
	function to_url($url, $data='')
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
  
